﻿Imports System.Data.SqlClient

Public Class frmInsuranceBrokersRegistryUpdate

    Public SNo As Integer

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        If Me.txtCompanyName.Text.Trim.Length = 0 Then
            MsgBox("Please fill in all mandatory fields")
            Exit Sub
        Else
            Try
                Me.Cursor = Cursors.WaitCursor

                If Me.txtEmail.Text.Trim.Length > 0 And _
                        Not System.Text.RegularExpressions.Regex.IsMatch(Me.txtEmail.Text.Trim, "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$") Then
                    Me.txtEmail.Clear()
                End If

                Dim cmd As New SqlCommand("Update InsuranceBrokers " & _
                                          "Set CompanyName=@CompanyName," & _
                                          "MobileNo=@MobileNo,Email=@Email,Address=@Address Where SNo=" & SNo, cnn)

                cnn.Open()

                'Add values
                cmd.Parameters.AddWithValue("@CompanyName", Me.txtCompanyName.Text.Trim)
                cmd.Parameters.AddWithValue("@MobileNo", Me.txtMobileNo.Text.Trim)
                cmd.Parameters.AddWithValue("@Email", Me.txtEmail.Text.Trim)
                cmd.Parameters.AddWithValue("@Address", Me.txtAddress.Text.Trim)

                cmd.ExecuteNonQuery()
                cnn.Close()

                Me.Cursor = Cursors.Default

                Me.Close()
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Private Sub frmInsuranceBrokersRegistryUpdate_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From InsuranceBrokers Where SNo=" & SNo, cnn)
            Dim Reader As SqlDataReader

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Me.txtCompanyName.Text = Reader.Item("CompanyName")
                Me.txtMobileNo.Text = Reader.Item("MobileNo")
                Me.txtEmail.Text = Reader.Item("Email")
                Me.txtAddress.Text = Reader.Item("Address")
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class